package com.hb.trade.api.routine.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.hb.core.pojo.ConsoleItemTreeEntity;
import com.hb.core.pojo.MenuTreeEntity;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.component.treebuilder.entity.TreeBuilder;
import org.w3c.dom.Document;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;

/**
 * 扩展公共工具类
 *
 * @author zhaojk
 */
@Slf4j
public class CommonUtil {

    /**
     * 将传入的list对象转换为tree对象
     *
     * @param list 被转换的集合, 其中的对象会被转换为json, 对象中必须要包含 id 和 parentId 属性
     * @return
     */
    public static JSONArray getConsoleItemTree(List<?> list) {
        if (CollUtil.isEmpty(list)) {
            return JSONUtil.createArray();
        }

        TreeBuilder<Long, ConsoleItemTreeEntity> treeBuilder = new TreeBuilder<>(true);
        treeBuilder.sourceOrgList(list, null, ConsoleItemTreeEntity.class);
        treeBuilder.setSortField("sortCode");
        return treeBuilder.build();
    }

    /**
     * 将传入的list对象转换为tree对象
     *
     * @param list 被转换的集合, 其中的对象会被转换为json, 对象中必须要包含 id 和 parentId 属性
     * @return
     */
    public static JSONArray getMenuTree(List<?> list) {
        if (CollUtil.isEmpty(list)) {
            return JSONUtil.createArray();
        }

        TreeBuilder<Long, MenuTreeEntity> treeBuilder = new TreeBuilder<>(false);
        treeBuilder.sourceOrgList(list, null, MenuTreeEntity.class);
        treeBuilder.setSortField("sortCode");
        return treeBuilder.build();
    }

    /**
     * 获取客户端地址
     *
     * @param request: 请求对象
     * @Description:
     * @Author: zhongdl
     * @Date:2021/12/6 9:53
     * @return: {@link String }
     **/
    public static String getClientIpAddress(HttpServletRequest request) {
        String clientIp = request.getHeader("x-forwarded-for");
        if (StrUtil.isEmpty(clientIp) || "unknown".equalsIgnoreCase(clientIp)) {
            clientIp = request.getHeader("Proxy-Client-IP");
        }
        if (StrUtil.isEmpty(clientIp) || "unknown".equalsIgnoreCase(clientIp)) {
            clientIp = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StrUtil.isEmpty(clientIp) || "unknown".equalsIgnoreCase(clientIp)) {
            clientIp = request.getRemoteAddr();
        }
        return clientIp;
    }

    /**
     * 将xml文件转换成JSONObject
     *
     * @param xmlContent xml文件内容
     * @return
     */
    public static JSONObject xmlToJSON(String xmlContent) {
        if (StrUtil.isEmpty(xmlContent)) {
            return JSONUtil.createObj();
        }
        Document document = XmlUtil.readXML(xmlContent);
        String toStr = XmlUtil.toStr(document);
        if (StrUtil.isNotEmpty(toStr)) {
            return JSONUtil.xmlToJson(toStr);
        }
        return JSONUtil.createObj();
    }

    /**
     * 将xml文件转换成JSONObject
     *
     * @param file 投标文件中开标一览表的xml文件对象
     * @return
     */
    public static JSONObject xmlFileToJSON(File file) {
        if (!FileUtil.exist(file)) {
            return JSONUtil.createObj();
        }
        Document document = XmlUtil.readXML(file);
        String toStr = XmlUtil.toStr(document);
        return xmlToJSON(toStr);
    }

}
